#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>

// 模拟一个长时间的计算任务
int calc_sqr(int num)
{
    int sqr = num * num;
    sleep(1);
    return sqr;
}

int main(int argc, char const *argv[])
{
    int nums[5] = {1, 2, 3, 4, 5};
    int sqrs[5];

    struct timeval start, end;
    gettimeofday(&start, NULL);

    // 计算多个数的平方
    for (int i = 0; i < 5; i++)
    {
        sqrs[i] = calc_sqr(nums[i]);
    }
    gettimeofday(&end, NULL);
    printf("时间：%ld \n", end.tv_sec - start.tv_sec);

    // 计算平方和
    int total = 0;
    for (int i = 0; i < 5; i++)
    {
        total += sqrs[i];
    }
    printf("Total: %d\n", total);
    
    return 0;
}




